<?php

class Comments_CommentsController extends Zend_Rest_Controller{
	public function init(){
        parent::init();
        $this->mapper = new Comments_Model_CommentsMapper( );
    }
	public function indexAction() {
        $start =(int) $this->_request->getParam('start',0);
        $limit =(int) $this->_request->getParam('limit',25);
        $query = $this->_request->getParam('query',null);
        if (null!=$query){
            $filterChain = new Zend_Filter ( );
            $query = $filterChain    ->addFilter ( new Zend_Filter_StripTags ( ) )
                            ->addFilter ( new Zend_Filter_StringTrim ( ) )
                            ->filter ( $query );
        }
		$result ['data'] = $this->mapper->fetchAll($query,$start,$limit);
        $result ['total'] = $this->mapper->count();
		$this->_helper->json ( $result );
	}
	public function getAction() {
		$this->_helper->viewRenderer->setRender('editor-js');
		$this->_helper->layout()->disableLayout();
	}
	public function postAction() {
        $request = $this->getRequest();
        $params = Zend_Json::decode ( $request->getRawBody () );
        $f = new Comments_Form_Comment();
        $params = $f->getValidValues($params['data']); //не проверяем всю форму, т.к. некоторые поля могут в POST запросе отсуствовать
        unset($params['Id']);
        //$params['JoinDate'] = date("Y-m-d");
        $result ['data'] = $params;
        try{
            $result ['data']['Id'] = $this->mapper->save($params);
            $result ['success'] = true;
        }
        catch(Exception $e)
        {
            $result ['success'] = false;
        }
        $this->_helper->json ( $result );
	}
	public function putAction() {
        $request = $this->getRequest();
		$params = Zend_Json::decode ( $request->getRawBody () );
        $f = new Comments_Form_Comment();
        $params = $f->getValidValues($params['data']); //не проверяем всю форму, т.к. некоторые поля могут в PUT запросе отсуствовать
        if (!isset($params['Id']))
            throw new Zend_Exception('Не установлен Id');
        try{
            $this->mapper->save($params);
            $result['success'] = true;
            if (isset($params['Publish'])){
                $nodeId = isset($params['NodeId'])?$params['NodeId']:$this->mapper->fetchNodeId($params['Id']);
                $m = new Admin_Model_TreeNodeMapper();
                $m->clearCache($nodeId);
            }
        }
        catch(Exception $e)
        {
            $result ['success'] = false;
        }
        $this->_helper->json ( $result );
	}
	
	public function deleteAction() {
		$request = $this->getRequest();
        $id = $request->getParam ( 'id', 0 );
        if ($id<1) {
            $this->_helper->json ( array('success'=>false, 'msg'=>'No Id'));
            return;
        }
        try{
            $this->mapper->delete($id);
            $this->_helper->json(array('success'=>true));
        }
        catch(Exception $e)
        {
            $this->_helper->json(array('success'=>false));
        }
	}


}

